package com.zmn.oms.admin.controller.work.modify;

import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.common.annotation.PageRequestCas;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.work.modify.must.OrderAssignDTO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.model.vo.work.OrderAssignVO;
import com.zmn.oms.zmn.business.interfaces.work.OrderWorkAssignBService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import com.zmn.oms.zmn.business.interfaces.work.autoflow.WorkFlowContextBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * 类描述：分单
 *
 * @author liuying
 * @date 2019/05/09 16:06
 */
@Controller
@RequestMapping("/work/modify")
public class WorkAssignController extends WorkBaseController {

    @Autowired
    private OrderWorkAssignBService orderWorkAssignBService;
    @Autowired
    private ZsOrderWorkOperatePermissionBService orderWorkOperatePermissionBService;
    @Autowired
    private WorkFlowContextBService workFlowContextBService;

    /**
     * 分单
     */
    @RequestMapping(value = "/assign")
    @PageRequestCas(type = OrderConsts.ORDER_OP_TYPE_ASSIGN)
    public ModelAndView assign(Long orderId, Long workId) {
        ModelAndView mv = new ModelAndView("work/modify/assign");

        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
        OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(orderId);
        List<OrderAssignVO> assignList = orderWorkAssignBService.listAssignCompany(orderWork, orderDetail, false);
        mv.addObject("orderId", orderId);
        mv.addObject("workId", workId);
        mv.addObject("assignList", assignList);

        return mv;
    }


    /**
     * 保存分单
     */
    @RequestMapping(value = "/save/assign")
    @ResponseBody
    public ResultDTO saveAssign(@RequestBody @Valid OrderAssignDTO orderAssignDTO) {

        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.getOperatePermission(orderAssignDTO.getOrderId(), orderAssignDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_ASSIGN);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }
        try {
            orderAssignDTO.setStatus(OrderStatusConsts.WORK_STATUS_ASSIGN);
            orderWorkAssignBService.saveAssign(orderAssignDTO);
            // 工单自动流转
            workFlowContextBService.asyncAutoFlow(orderAssignDTO.getOrderId(),orderAssignDTO.getWorkId());
            return ResultDTO.success();
        } catch (OmsBaseException e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

}
